#include "key.h"
#include "stm32f10x_exti.h"
#include "cal_snake.h"

volatile uint8_t dir_buffer = 0; // 方向缓冲

void Key_Init(void)
{
    GPIO_InitTypeDef GPIO_InitStruct;
    EXTI_InitTypeDef EXTI_InitStruct;
    NVIC_InitTypeDef NVIC_InitStruct;

    // 时钟使能
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_AFIO, ENABLE);

    // GPIO配置（下拉输入）
    GPIO_InitStruct.GPIO_Pin = KEY_UP_PIN | KEY_DOWN_PIN | KEY_LEFT_PIN | KEY_RIGHT_PIN;
    GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IPD;
    GPIO_Init(KEY_PORT, &GPIO_InitStruct);

    // 外部中断配置
    GPIO_EXTILineConfig(GPIO_PortSourceGPIOA, GPIO_PinSource0);
    GPIO_EXTILineConfig(GPIO_PortSourceGPIOA, GPIO_PinSource1);
    GPIO_EXTILineConfig(GPIO_PortSourceGPIOA, GPIO_PinSource2);
    GPIO_EXTILineConfig(GPIO_PortSourceGPIOA, GPIO_PinSource3);

    EXTI_InitStruct.EXTI_Line = EXTI_Line0 | EXTI_Line1 | EXTI_Line2 | EXTI_Line3;
    EXTI_InitStruct.EXTI_Mode = EXTI_Mode_Interrupt;
    EXTI_InitStruct.EXTI_Trigger = EXTI_Trigger_Rising;  // 上升沿触发
    EXTI_InitStruct.EXTI_LineCmd = ENABLE;
    EXTI_Init(&EXTI_InitStruct);

    // NVIC配置
    NVIC_InitStruct.NVIC_IRQChannel = EXTI0_IRQn;
    NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 0x01;
    NVIC_InitStruct.NVIC_IRQChannelSubPriority = 0x01;
    NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE;
    NVIC_Init(&NVIC_InitStruct);
    
    // EXTI1配置（DOWN键）
    NVIC_InitStruct.NVIC_IRQChannel = EXTI1_IRQn;
    NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 0x01;
    NVIC_InitStruct.NVIC_IRQChannelSubPriority = 0x02;
    NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE;
    NVIC_Init(&NVIC_InitStruct);

    // EXTI2配置（LEFT键）
    NVIC_InitStruct.NVIC_IRQChannel = EXTI2_IRQn;
    NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 0x01;
    NVIC_InitStruct.NVIC_IRQChannelSubPriority = 0x03;
    NVIC_Init(&NVIC_InitStruct);

    // EXTI3配置（UP键）
    NVIC_InitStruct.NVIC_IRQChannel = EXTI3_IRQn;
    NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 0x01;
    NVIC_InitStruct.NVIC_IRQChannelSubPriority = 0x04;
    NVIC_Init(&NVIC_InitStruct);
}

// 外部中断处理函数
void EXTI0_IRQHandler(void)    // UP键
{
    if(EXTI_GetITStatus(EXTI_Line0) != RESET)
    {
        // 检测实际电平防止抖动
        if(GPIO_ReadInputDataBit(KEY_PORT, KEY_UP_PIN))
        {
            if(current_dir != DOWN)
                next_dir = UP;//新的值赋值在这里（在main里方向进行传递，在snake里进行判断）
        }
        EXTI_ClearITPendingBit(EXTI_Line0);
    }
}

void EXTI1_IRQHandler(void)    // DOWN键
{
    if(EXTI_GetITStatus(EXTI_Line1) != RESET)
    {
        if(GPIO_ReadInputDataBit(KEY_PORT, KEY_DOWN_PIN))
        {
            if(current_dir != UP)
                next_dir = DOWN;
        }
        EXTI_ClearITPendingBit(EXTI_Line1);
    }
}

void EXTI2_IRQHandler(void)    // LEFT键
{
    if(EXTI_GetITStatus(EXTI_Line2) != RESET)
    {
        if(GPIO_ReadInputDataBit(KEY_PORT, KEY_LEFT_PIN))
        {
            if(current_dir != RIGHT)
                next_dir = LEFT;
        }
        EXTI_ClearITPendingBit(EXTI_Line2);
    }
}

void EXTI3_IRQHandler(void)    // RIGHT键
{
    if(EXTI_GetITStatus(EXTI_Line3) != RESET)
    {
        if(GPIO_ReadInputDataBit(KEY_PORT, KEY_RIGHT_PIN))
        {
            if(current_dir != LEFT)
                next_dir = RIGHT;
        }
        EXTI_ClearITPendingBit(EXTI_Line3);
    }
}
